function Circle_Code() {
	var buffer = new StringBuffer();
	buffer.append('<div class="circle">');
		buffer.append('<div class="wallpaper"><img id="wallpaper" src="img/bg.gif" height="220"/><div class="realname">'+User.RealName+'</div><div class="headimg" id="headimg"><img src="'+User.HeadImg+'"/></div></div>');
	buffer.append('</div>');
	return buffer.toString();
};

function Circle_Data(data)
{
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
	try{
	info.Context=decodeURI(info.Context);
	}catch(e){}
	buffer.append('<div class="circlelist border-bottom" id="'+info.ID+'">');
			buffer.append('<div class="headimg"><img src="'+info.HeadImg+'"/></div>');
			buffer.append('<div class="right" style="width:'+(swidth-70)+'px">');
				buffer.append('<div class="realname">'+info.RealName+'</div>');
				buffer.append('<div class="context">'+info.Context+'</div>');
				buffer.append('<div class="aimg">');
				$(info.Imgs).each(function(k,img)
				{
					buffer.append('<figure class="asquare" style="width:'+(swidth-130)/3+'px; heigth:'+(swidth-130)/3+'px; margin-right:5px; float:left; display:block;" itemscope itemtype="http://schema.org/ImageObject"><a src="'+img.Img1000+'" itemprop="contentUrl" data-size="'+img.Width+'x'+img.Height+'"><img src="' + img.Img200 + '" width="'+(swidth-130)/3+'" height="'+(swidth-130)/3+'" itemprop="thumbnail" alt="Image description"/></a></figure>');
				});
				buffer.append('</div>');
				if(info.Address!=""){
				buffer.append('<div class="aposition"><div class="address">'+info.Address+'</div></div>');
				}
				buffer.append('<div class="tool"><div class="time">'+info.CreateTime+' &nbsp;&nbsp;');
				if(info.UserID==User.ID)
				{
					buffer.append('<a>删除</a>');
				}
				buffer.append('</div><div class="reply" id="'+info.ID+'" userid="'+info.UserID+'" realname="'+info.RealName+'" praise="'+info.HasPraised+'"><img src="img/reply.png"/></div></div>');
				if(info.PraiseName!="" || info.ReplyNum>0)
				{
					var border_bottom="";
					if(info.PraiseName!="" && info.ReplyNum>0)
					{
						border_bottom="border-bottom2";
					}
					buffer.append('<div class="replyandpraise">');
						buffer.append('<div class="praise_panel '+border_bottom+'">');
						if(info.PraiseName!="")
						{
							buffer.append('<div class="xin"><img src="img/praise2.png" width="14" /></div>');
						}else{
							buffer.append('<div class="xin hidden"><img src="img/praise2.png" width="14" /></div>');
						}
						buffer.append('<div class="ren">'+info.PraiseName+'</div></div>');
						buffer.append('<div class="reply_panel">');
							$(info.ReplyList).each(function(i,reply){
								try{
								reply.Context=decodeURI(reply.Context);
								}catch(e){}
								if(reply.ParentID==0)
								{
									buffer.append('<div class="replyline" id="'+reply.ID+'" userid="'+reply.UserID+'" tableid="" username="'+reply.UserName+'" sid="'+info.ID+'" parentid="'+reply.ParentID+'"><font>'+reply.UserName+'：</font>'+reply.Context+'</div>');
								}else{
									buffer.append('<div class="replyline" id="'+reply.ID+'" userid="'+reply.UserID+'" tableid="" username="'+reply.UserName+'" sid="'+info.ID+'" parentid="'+reply.ParentID+'"><font>'+reply.UserName+'</font>回复<font>'+reply.ToUserName+'：</font>'+reply.Context+'</div>');
								}
							});
						buffer.append('</div>');
					buffer.append('</div>');
				}
			buffer.append('</div>');
		buffer.append('</div>');
	});
	return buffer.toString();
};

var cc=0;
function Init_Circle()
{
	mui("#contentlist2").off("tap",".reply");
	mui("#contentlist2").on("tap",".reply",function(){
		cc=1;
		obj=$(this);
		OpenReply(this,function(){
			get("circlepraise").addEventListener("tap",function(){
				Close_RWindow();
				if(obj.attr("praise")=="true")
				{
					return;
				}
				var data = {};
				data.act = "Praise_Circle";
				data.source=source;
				data.version=version;
				data.userid=userid;
				data.comid=comid;
				data.touserid=obj.attr("userid");
				data.sid=obj.attr("id");
				data.title="赞";
				data.sign=GetSign(data.act);
				
				mui.getJSON(index_domain, data, function(json) {
				if (json.state) {
									
					plus.nativeUI.toast("已点赞");
					obj.attr("praise","true");
					if(obj.parent().parent().find(".replyandpraise").length==0)
					{
						obj.parent().parent().append('<div class="replyandpraise"><div class="praise_panel"><div class="xin"><img src="img/praise2.png" width="14" /></div><div class="ren">'+User.RealName+'</div></div><div class="reply_panel"></div></div>');
					}else
					{
						var oldpraise=obj.parent().parent().find(".replyandpraise .praise_panel .ren").html();
						if(obj.parent().parent().find(".reply_panel").html()!="")
						{
							obj.parent().parent().find(".praise_panel").addClass("border-bottom2");
						}
						
						obj.parent().parent().find(".replyandpraise .praise_panel .ren").html((oldpraise+","+User.RealName).TrimStart(","));
						obj.parent().parent().find(".replyandpraise .praise_panel .xin").removeClass("hidden");
					}
				}
					
				});
			});
			get("circlereply").addEventListener("tap",function(){
				Close_RWindow();
				var html='<div style="height:auto;"><textarea id="replycontext" placeholder="回复'+obj.attr("realname")+'"></textarea>';
				html+='<div class="mtool"><div class="mbutton"><button id="reply" class="button blue right5">回复</button><button id="close" class="button gray right5">取消</button></div></div></div>';
				OpenMeng(html);
				get("reply").addEventListener("tap", function() {
							var data = {};
						    data.act = "Save_Reply";
						    data.sid = obj.attr("id");
						    data.parentid = 0 ;
						    data.tableid = 26;
						    data.context = $("#replycontext").val();
						    data.userid = User.ID;
						    data.comid = User.ComID;
						    data.touserid = obj.attr("userid");
						    data.imgs = "";
						    data.source = source;
						    data.version = version;
						    if(data.context.Trim()=="")
						    {
						    	plus.nativeUI.toast("请输入内容");
						    	return;
						    }
						    data.sign=GetSign(data.act);
						    CloseMeng(); 
						    mui.getJSON(index_domain, data, function(json) {
						    	var reply=json.data;
								if (json.state) { 
									try{
									json.data.Context=decodeURI(json.data.Context);
									}catch(e){}
						            plus.nativeUI.toast("回复成功");
						            if(obj.parent().parent().find(".replyandpraise").length==0)
									{
										obj.parent().parent().append('<div class="replyandpraise"><div class="praise_panel"><div class="xin hidden"><img src="img/praise2.png" width="14" /></div><div class="ren"></div></div><div class="reply_panel"><div class="replyline" id="'+reply.ID+'" userid="'+reply.UserID+'" username="'+reply.UserName+'" sid="'+reply.SID+'" parentid="'+reply.ParentID+'"><font>'+User.RealName+'：</font>'+json.data.Context+'</div></div></div>');
									}else
									{
										if(obj.parent().parent().find(".praise_panel .ren").html()!="")
										{
											obj.parent().parent().find(".praise_panel").addClass("border-bottom2");
										}
										obj.parent().parent().find(".reply_panel").append('<div class="replyline" id="'+reply.ID+'" userid="'+reply.UserID+'" username="'+reply.UserName+'" sid="'+reply.SID+'" parentid="'+reply.ParentID+'"><font>'+User.RealName+'：</font>'+json.data.Context+'</div>');
									}
								}
							});
				});
			});
		});
	});	
	mui("#contentlist2").off("tap",".time");
	mui("#contentlist2").on("tap",".time",function(){
		obj=$(this).parent().parent().parent();
        	plus.nativeUI.confirm("确定要删除吗？", function(event){
				if(event.index==1)
				{
					var data = {};
				    data.act = "Delete_Circle";
				    data.ids = obj.attr("id");
				    data.userid=userid;
					data.comid=comid;
					data.source=source;
					data.sign=GetSign(data.act);
				    mui.getJSON(index_domain, data, function(json) {
				    	plus.nativeUI.toast(json.msg);
						if(json.state)
						{
							obj.fadeOut(500);
						}
				    });	
				}
			},"提示", ["取消","确认"]); 
	});
	
	get("wallpaper").addEventListener("tap",function(){
		plus.nativeUI.toast("更多体验随后大波儿袭来！");
	});
	get("headimg").addEventListener("tap",function(){
		plus.nativeUI.toast("更多体验随后大波儿袭来！");
	});
};

function Close_RWindow(){
	if($("#divmsg300").length>0){
		$("#divmsg300").css("left",(swidth-35)+"px").css("width","0px").attr("state","close");
	}
};

function Init_Drag(){
	get("divmsg2").addEventListener("dragstart",function(){
		if(cc==0)
		{
			Close_RWindow();
	    }
	    cc=0;
	});
	get("divmsg2").addEventListener("tap",function(){
		if(cc==0)
		{
			Close_RWindow();
	    }
	    cc=0;
	});
};


function Receive_Circle(pageindex){
			if(pageindex==1)
			{
				$("#contentlist2").html(Circle_Code());
			}
			var data = {};
			data.act = "Find_Circle";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.sign=GetSign(data.act);
			
			mui.getJSON(index_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist2").html(Circle_Code()+Circle_Data(json.data.list));
					}else{
						$("#contentlist2").append(Circle_Data(json.data.list));
					}
					setTimeout(function(){
						Init_Circle();
						myScroll1.refresh();
						initPhotoSwipeFromDOM('.circlelist');
						Init_ReplyList();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading2").html("");
					}else{
						$("#loading2").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					
					if(pageindex==1)
					{
						$("#contentlist2").html(Circle_Code());
						$("#loading2").html("赶快去分享吧"); 
					}
					else
					{
						$("#loading2").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg2").attr("pageindex"))+1;
         		$("#divmsg2").attr("pageindex",pageindex);
			});
};

function Receive_Circle2(){
			pageindex=parseInt($("#divmsg2").attr("pageindex"));
			var data = {};
			data.act = "Find_Circle";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=1;
			data.source=source;
			data.pagesize=2 * pageindex;
			data.sign=GetSign(data.act);
			
			request = getJSON(index_domain, data, function(json) {
				if(json.data.list.length>0)
				{
					$("#contentlist2").html(Circle_Code()+Circle_Data(json.data.list));
					setTimeout(function(){
						Init_Circle();
						myScroll1.refresh();
						initPhotoSwipeFromDOM('.circlelist');
						Init_ReplyList();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.list.length!=data.pagesize)
					{
						$("#loading2").html("");
					}else{
						$("#loading2").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					if(pageindex==1)
					{
						$("#contentlist2").html(Circle_Code());
						$("#loading2").html("赶快去分享吧"); 
					}
					else
					{
						$("#loading2").html("没有更多了");
					}
				}
				pageindex++;
         		$("#divmsg2").attr("pageindex",pageindex);
			});
};

function Init_ReplyList(){
				//二级回复
				mui(".reply_panel").off("tap",".replyline");
				mui(".reply_panel").on("tap",".replyline",function() {
					var replylist = $(this);
						if(userid+""!=replylist.attr("userid"))
						{
							OpenMeng('<div style="height:200px;"><textarea id="replycontext" placeholder="回复'+replylist.attr("username")+'"></textarea><div class="mtool"><div class="mbutton"><button id="reply" class="button blue right5">回复</button><button id="close" class="button gray right5">取消</button></div></div></div>');
							get("reply").addEventListener("tap", function() {
								var data = {};
							    data.act = "Save_Reply";
							    data.sid = replylist.attr("sid");
							    data.parentid = replylist.attr("parentid");
							    if(data.parentid==0)
							    {
							    	data.parentid=replylist.attr("id");
							    }
							    data.tableid = 26;
							    data.context = $("#replycontext").val();
							    data.userid = User.ID;
							    data.comid = User.ComID;
							    data.touserid = replylist.attr("userid");
							    data.imgs = "";
							    data.source = source;  
							    data.version = version;
							    data.sign=GetSign(data.act);
							    if(data.context.Trim()=="")
							    {
							    	plus.nativeUI.toast("请输入内容");
							    	return;
							    }
							    CloseMeng();
							    mui.getJSON(index_domain, data, function(json) {
							    	var reply=json.data;
									if (json.state) {
										replylist.parent().append('<div class="replyline" id="'+reply.ID+'" userid="'+reply.UserID+'" username="'+reply.UserName+'" sid="'+reply.SID+'" parentid="'+reply.ParentID+'"><font>'+User.RealName+'</font>回复<font>'+replylist.attr("username")+'：</font>'+data.context+'</div>');
							            plus.nativeUI.toast("回复成功");
							            
							        } else {
							            
							        }
							        
								});
							});
						}else
						{
							var data = {};
							data.id = replylist.attr("id");
							data.ids = data.id;
						    data.sid = replylist.attr("sid");
						    data.tableid = 26;
						    data.act = "Delete_Reply";
						    data.source = source; 
						    data.version = version;
						    data.userid=userid;
							data.comid=comid;
							data.sign=GetSign(data.act);
						    plus.nativeUI.confirm("确定要删除该回复吗？", function(event){
							if(event.index==1)
							{ 
								mui.getJSON(index_domain, data, function(json) {
									if (json.state) {
										replylist.hide(500);
							            plus.nativeUI.toast("删除成功");
							        } else {
							            plus.nativeUI.toast("删除失败");
							        }
								}); 
							}
							},"提示", ["取消","确认"]); 
						}
					});
					
};


function CircleAdd_Data() { 
	var buffer = new StringBuffer();
	buffer.append('<div class="publish border-bottom">');
	buffer.append('<textarea id="txt_context" placeholder="您想说点什么..." onkeyup=Context_Change(this,"context",26)></textarea>');
	buffer.append('<div class="addphoto border-bottom" style="width:'+(swidth-20)+'px;"><div id="img_src"><div id="addphoto" style="float:left"><img class="add" src="img/add-photo.png" style="width:'+(swidth-70)/4+'px;height:'+(swidth-70)/4+'px;" /><div class="imgload"></div></div></div></div>');
	buffer.append('<div id="currposition" class="position border-bottom"><div class="icon"><img src="img/position.png"/></div><div id="address" lat="0" lng="0" class="address">所在位置</div><div class="arrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="position border-bottom" style="float:left; height:15px; background-color:#f3f3f3;"></div>');
	buffer.append('<div id="sendrange" class="position users border-bottom" show="2" touserid="" todepartid="" tocomid="'+comid+'" style="padding-left:20px;"><div class="inner" style="width:'+(swidth-20)+'px;"><div class="icon"><img src="img/client.png"/></div><div id="sendrange" class="address"><div class="long" style="width:25%;">谁可以看</div><div class="long" style="width:75%;"><input id="traveluser" placeholder="全公司" type="text" value="" readonly="readonly"/></div></div><div class="arrow"><img src="img/arrow.png"/></div></div></div>');
	buffer.append('</div>');
	return buffer.toString();
};

function Init_CircleAdd(){
	get("currposition").addEventListener("tap",function(){
			$("#address").html("正在定位...");			
			GetNewPos(function(e){
      			$("#address").html(e.address);
      			if(e.address!=null && e.address!="" && e.address!="null")
      			{
      				$("#address").attr("lat",e.latitude);
      				$("#address").attr("lng",e.longitude);
      			}else
      			{
      				$("#address").html("获取位置信息失败");
      			}
      		});
      		setTimeout(function(){
				if($("#address").html()=="正在定位..." )
				{
					$("#address").html("获取位置信息失败");
				}
			},5000);
	});
	
	get("addphoto").addEventListener('tap', function(event) {
				//get("share_context").blur();
						var btnArray = [{
							title: "拍照"
						}, {
							title: "从相册选择"
						}];
						plus.nativeUI.actionSheet({
							title: "选择照片",
							cancel: "取消",
							buttons: btnArray
						}, function(e) {
							var index = e.index;
							switch (index) {
								case 0:
									break; 
								case 1:
									var cmr = plus.camera.getCamera();
									cmr.captureImage(function(path) {
										at_count=0;
										ResizeImg("file://" + plus.io.convertLocalFileSystemURL(path),1);
										Init_ImgPreview();
									}, function(err) {});
									break;
								case 2:
									plus.gallery.pick(function(e) {
										//get("img_panel").style.display="";
										at_count=0;
										for(var i in e.files){
											ResizeImg(e.files[i],e.files.length);
											Init_ImgPreview();
								    	}
									}, function(err) {}, {filter:"image",multiple:true,maximum:9});
									break;
							}
						}); 
					}, false);
					
					
					
	mui(".publish").on("tap",".users",function(){
		obj10=$(this);
		OpenWindow(4,"选择发送对象","确定",function(){
				var str1="",str2="",str3="";
				touserid="";todepartid="";tocomid="";
      			var count=0;
      			mui.each(mui("#userlist input"),function(i,info){
      				if(info.checked)
      				{
      					count++;
      					str1 += info.getAttribute("val")+",";
      					touserid += info.value+",";
      				}
      			});
      			if(count>3){str1=count+"个同事,"}
      			
      			count=0;
      			mui.each(mui("#departlist .onlydepart input"),function(i,info){
      				if(info.checked)
      				{
      					count++;
      					str2 += info.getAttribute("val")+",";
      					todepartid += info.value+",";
      				}
      			});
      			if(count>3){str2=count+"个部门,"}
      			
      			mui.each(mui("#departlist .allcompany input"),function(i,info){
      				if(info.checked)
      				{
      					str3 += info.getAttribute("val")+",";
      					tocomid += info.value+",";
      				}
      			});
      			var str = str1+str2+str3;
      			if(str=="")
      			{
      				obj10.find("input").val("");
      			}else
      			{
      				obj10.find("input").val(str.substring(0,str.length-1));
      			}
      			
      			obj10.attr("touserid",touserid).attr("todepartid",todepartid).attr("tocomid",tocomid);
      			CloseWindow();
		});
		
		$("#contentlist4").html(CheckMate_Data());
		var data = {};
		data.act = "DepartAndUser_Data";
		data.userid=userid;
		data.comid=comid;
		data.source=source;
		data.sign=GetSign(data.act);
		
		mui.getJSON(admin_domain, data, function(json) {
			Bind_CheckMate(json.data.user,json.data.depart);
			Init_CheckMate();
		});
		if(obj10.attr("show")=="1")
      	{
      		$(".selectbar").hide();
      	}else
      	{
			$(".selectbar").show();  				
      	}
		
	});


};


function Save_Circle()
{
	var data={};
	var sendrange=$("#sendrange");
	var context=$("#txt_context").val();
	if(context.Trim()=="")
	{
		plus.nativeUI.toast("请写点什么吧");
		return;
	}
	if(context.length>300)
	{
		plus.nativeUI.toast("不能超过300个字哦");
		return;
	}
	data.act="Save_Circle";
	data.context=context;
	data.touserid=sendrange.attr("touserid");
	data.todepartid=sendrange.attr("todepartid");
	data.tocomid=sendrange.attr("tocomid");
	var coord=get("address");
	data.lat=coord.getAttribute("lat");
	data.lng=coord.getAttribute("lng");
	if(coord.innerHTML!="所在位置" && coord.innerHTML!="获取位置信息失败")
	{
		data.address=coord.innerHTML;
	}else
	{
		data.address="";
	}
	data.source=source;
	data.version=version;
	data.userid=userid;
	data.comid=comid;
	data.sign=GetSign(data.act); 
	upload(function(imgs){
		if(imgs!="")
		{
			data.imgs=eval(imgs).join(",");
		}else
		{
			data.imgs="";
		}
		plus.nativeUI.showWaiting( "正在保存..." );
	      			mui.getJSON(index_domain, data, function(json){
	      				plus.nativeUI.closeWaiting();
	      				plus.nativeUI.toast(json.msg);
	      				if(json.state)
	      				{
	      					CloseWindow();
	      					Receive_Circle(1);
	      				}
	    });
	});
};



